package com.cuixichang.minimalism.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cuixichang.minimalism.core.mybatis.cnstants.BaseFieldNames;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import com.cuixichang.minimalism.mapper.IRoleMapper;
import com.cuixichang.minimalism.transfer.dto.IRoleDTO;
import com.cuixichang.minimalism.transfer.factory.IRoleAssembler;
import com.cuixichang.minimalism.transfer.po.IRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.time.LocalDateTime;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.function.Function;
import org.springframework.stereotype.Service;

/**
 * 角色信息管理表表服务实现类
 *
 * @author cuixichang
 * @date 2024-12-18
 */
@Service
public class IRoleServiceImpl implements IRoleService {

    @Autowired
    private IRoleMapper iRoleMapper;

    @Override
    public IRoleDTO selectById(Long id){
        IRole iRole = iRoleMapper.selectById(id);
        return IRoleAssembler.INSTANCT.convertDto(iRole);
    }

    @Override
    public IRoleDTO selectOne(IRoleDTO iRoleDTO){
        Assert.notNull(iRoleDTO,"参数为空,请检查");
        IRole iRole = IRoleAssembler.INSTANCT.convertPo(iRoleDTO);
        IRole result = iRoleMapper.selectOne(new QueryWrapper<>(iRole));
        return IRoleAssembler.INSTANCT.convertDto(result);
    }

    @Override
    public List<IRoleDTO> selectByIds(List<Long> ids) {
        Assert.notEmpty(ids,"参数为空,请检查");
        List<IRole> results = iRoleMapper.selectBatchIds(ids);
        return results.stream().map(IRoleAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public List<IRoleDTO> selectList(IRoleDTO iRoleDTO){
        return this.selectList(iRoleDTO, (iRole, queryWrapper) -> {});
    }

    @Override
    public List<IRoleDTO> selectList(IRoleDTO iRoleDTO, BiConsumer<IRole, QueryWrapper<IRole>> consumer){
        Assert.notNull(iRoleDTO,"参数为空,请检查");
        IRole iRole = IRoleAssembler.INSTANCT.convertPo(iRoleDTO);
        QueryWrapper<IRole> queryWrapper = new QueryWrapper<>(iRole);
        if(Objects.nonNull(consumer)){
            consumer.accept(iRole, queryWrapper);
        }
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        List<IRole> results = iRoleMapper.selectList(queryWrapper);
        return results.stream().map(IRoleAssembler.INSTANCT::convertDto).collect(Collectors.toList());
    }

    @Override
    public Page<IRoleDTO> selectPage(IRoleDTO iRoleDTO,long pageSize,long pageNo){
        return this.selectPage(iRoleDTO, pageSize, pageNo, (iRole, queryWrapper) -> {});
    }

    @Override
    public Page<IRoleDTO> selectPage(IRoleDTO iRoleDTO,long pageSize,long pageNo, BiConsumer<IRole, QueryWrapper<IRole>> consumer){
        Assert.notNull(iRoleDTO,"参数为空,请检查");
        IRole iRole = IRoleAssembler.INSTANCT.convertPo(iRoleDTO);
        QueryWrapper<IRole> queryWrapper = new QueryWrapper<>(iRole);
        if(Objects.nonNull(consumer)) consumer.accept(iRole, queryWrapper);
        queryWrapper.orderBy(true,true,BaseFieldNames.SORT).orderBy(true,false,BaseFieldNames.UPDATE_TIME);
        Page<IRole> page = iRoleMapper.selectPage(new Page<>(pageNo,pageSize),queryWrapper);
        Page<IRoleDTO> result = Page.of(page.getCurrent(),page.getSize(),page.getTotal());
        result.setRecords(page.getRecords().stream()
                .map(IRoleAssembler.INSTANCT::convertDto).collect(Collectors.toList()));
        return result;
    }

    @Override
    public int deleteById(Long id){
        return iRoleMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(List<Long> ids){
        return iRoleMapper.deleteBatchIds(ids);
    }

    @Override
    public int deleteByCondition(IRoleDTO iRoleDTO){
        return deleteByCondition(iRoleDTO, (iRole, queryWrapper)-> {});
    }

    @Override
    public int deleteByCondition(IRoleDTO iRoleDTO, BiConsumer<IRole, QueryWrapper<IRole>> consumer){
        Assert.notNull(iRoleDTO,"参数为空,请检查");
        IRole iRole = IRoleAssembler.INSTANCT.convertPo(iRoleDTO);
        QueryWrapper<IRole> queryWrapper = new QueryWrapper<>(iRole);
        if(Objects.nonNull(consumer)) {
            consumer.accept(iRole, queryWrapper);
        }
        return iRoleMapper.delete(queryWrapper);
    }

    @Override
    public IRoleDTO insert(IRoleDTO iRoleDTO){
        return insertHasOption(iRoleDTO, (IRoleDTO) -> null);
    }

    @Override
    public IRoleDTO insertHasOption(IRoleDTO iRoleDTO, Function<IRoleDTO,IRoleDTO> preOption){
        Assert.notNull(iRoleDTO,"参数为空,请检查");
        iRoleDTO.check();
        IRole iRole = IRoleAssembler.INSTANCT.convertPo(iRoleDTO);
        Long createLoginId = iRole.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRole.setUpdateLoginId(createLoginId);
        LocalDateTime localDateTime = LocalDateTime.now();
        iRole.setCreateTime(localDateTime);
        iRole.setUpdateTime(localDateTime);
        Boolean isEnable = iRole.getIsEnable();
        if(Objects.isNull(isEnable)) iRole.setIsEnable(true);
        Integer sort = iRole.getSort();
        if(Objects.isNull(sort)) iRole.setSort(0);
        if(Objects.nonNull(preOption)){
            IRoleDTO apply = preOption.apply(iRoleDTO);
            if(Objects.nonNull(apply)) {
              return apply;
            }
        }
        int insert = iRoleMapper.insert(iRole);
        Assert.isTrue(insert>0,"插入失败");
        return IRoleAssembler.INSTANCT.convertDto(iRole);
    }

    @Override
    public int updateById(IRoleDTO iRoleDTO){
         return updateById(iRoleDTO, (iRole, updateWrapper) -> {});
    }

    @Override
    public int updateById(IRoleDTO iRoleDTO, BiConsumer<IRole, UpdateWrapper<IRole>> consumer){
        Assert.notNull(iRoleDTO,"参数为空,请检查");
        Long id = iRoleDTO.getId();
        Assert.notNull(id,"主键为空,请检查");
        IRole iRole = IRoleAssembler.INSTANCT.convertPo(iRoleDTO);
        Long createLoginId = iRole.getCreateLoginId();
        Assert.notNull(createLoginId,"创建人ID为空,请检查");
        iRole.setCreateLoginId(null);
        iRole.setUpdateLoginId(createLoginId);
        iRole.setUpdateTime(LocalDateTime.now());
        UpdateWrapper<IRole> updateWrapper = new UpdateWrapper<IRole>().eq(BaseFieldNames.ID, id);
        if(Objects.nonNull(consumer)) consumer.accept(iRole,updateWrapper);
        return iRoleMapper.update(iRole,updateWrapper);
    }
}
